package pck_gui;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import pck_pojos.Cls_Profesion;
import pck_utilitarios.Cls_Funciones;
import pck_utilitarios.Cls_InvocaWebService;
import pck_utilitarios.Cls_Parametros;
import pck_utilitarios.JP_Modelo;
import pck_utilitarios.Utiles_Panel;
import udprrhh.UDPRRHHView;

/**
 * @author supermiguelon
 */
public class JP_Profesiones_Mantenimiento extends JP_Modelo {

    private String opcion;

    /**
     * Creates new form JP_Profesiones_Mantenimiento
     */
    public JP_Profesiones_Mantenimiento() {
        initComponents();
        BTN_Cancelar.setEnabled(false);
    }

    public void resetearFormulario() {
        limpiarCampos();
        TXT_Profesion.setEditable(false);
        TXT_Abreviatura.setEditable(false);
        BTN_Buscar.setEnabled(true);
        BTN_Nuevo.setEnabled(true);
        BTN_Guardar.setEnabled(false);
        BTN_Modificar.setEnabled(false);
        BTN_Cancelar.setEnabled(false);
    }

    public void obtenerProfesion(Cls_Profesion profesion) {
//        String cadena = "select cod_profesion,descripcion,abreviatura from profesion ";
//        cadena = cadena + "where cod_profesion = '"+codigo+"'";
//        try{
//            Connection cn = Cls_Conexion.obtenerConexion();
//            Statement st = cn.createStatement();
//            ResultSet rs = st.executeQuery(cadena);
//            while(rs.next()){
//                TXT_Codigo.setText(rs.getString("cod_profesion"));
//                TXT_Profesion.setText(rs.getString("descripcion"));
//                TXT_Abreviatura.setText(rs.getString("abreviatura"));
//            }
//            cn.close();
//            st.close();
//            rs.close();
//        }catch(Exception e){
//            System.out.println(e.getMessage());
//        }
        TXT_Codigo.setText(profesion.getCodigo());
        TXT_Profesion.setText(profesion.getProfesion());
        TXT_Abreviatura.setText(profesion.getAbreviatura());
        BTN_Modificar.setEnabled(true);
        BTN_Cancelar.setEnabled(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        BTN_Nuevo = new javax.swing.JButton();
        BTN_Guardar = new javax.swing.JButton();
        BTN_Modificar = new javax.swing.JButton();
        BTN_Cancelar = new javax.swing.JButton();
        TXT_Codigo = new javax.swing.JTextField();
        TXT_Profesion = new javax.swing.JTextField();
        TXT_Abreviatura = new javax.swing.JTextField();
        BTN_Buscar = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(udprrhh.UDPRRHHApp.class).getContext().getResourceMap(JP_Profesiones_Mantenimiento.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);
        jLabel1.setBounds(50, 40, 60, 14);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(50, 70, 70, 14);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jLabel3.setName("jLabel3"); // NOI18N
        jPanel1.add(jLabel3);
        jLabel3.setBounds(50, 100, 80, 14);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel2.border.title"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(null);

        BTN_Nuevo.setText(resourceMap.getString("BTN_Nuevo.text")); // NOI18N
        BTN_Nuevo.setName("BTN_Nuevo"); // NOI18N
        BTN_Nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_NuevoActionPerformed(evt);
            }
        });
        jPanel2.add(BTN_Nuevo);
        BTN_Nuevo.setBounds(13, 30, 90, 23);

        BTN_Guardar.setText(resourceMap.getString("BTN_Guardar.text")); // NOI18N
        BTN_Guardar.setEnabled(false);
        BTN_Guardar.setName("BTN_Guardar"); // NOI18N
        BTN_Guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_GuardarActionPerformed(evt);
            }
        });
        jPanel2.add(BTN_Guardar);
        BTN_Guardar.setBounds(110, 30, 90, 23);

        BTN_Modificar.setText(resourceMap.getString("BTN_Modificar.text")); // NOI18N
        BTN_Modificar.setEnabled(false);
        BTN_Modificar.setName("BTN_Modificar"); // NOI18N
        BTN_Modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_ModificarActionPerformed(evt);
            }
        });
        jPanel2.add(BTN_Modificar);
        BTN_Modificar.setBounds(210, 30, 90, 23);

        BTN_Cancelar.setText(resourceMap.getString("BTN_Cancelar.text")); // NOI18N
        BTN_Cancelar.setName("BTN_Cancelar"); // NOI18N
        BTN_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_CancelarActionPerformed(evt);
            }
        });
        jPanel2.add(BTN_Cancelar);
        BTN_Cancelar.setBounds(310, 30, 90, 23);

        jPanel1.add(jPanel2);
        jPanel2.setBounds(30, 130, 410, 70);

        TXT_Codigo.setEditable(false);
        TXT_Codigo.setName("TXT_Codigo"); // NOI18N
        jPanel1.add(TXT_Codigo);
        TXT_Codigo.setBounds(140, 40, 50, 20);

        TXT_Profesion.setEditable(false);
        TXT_Profesion.setName("TXT_Profesion"); // NOI18N
        jPanel1.add(TXT_Profesion);
        TXT_Profesion.setBounds(140, 70, 250, 20);

        TXT_Abreviatura.setEditable(false);
        TXT_Abreviatura.setName("TXT_Abreviatura"); // NOI18N
        jPanel1.add(TXT_Abreviatura);
        TXT_Abreviatura.setBounds(140, 100, 80, 20);

        BTN_Buscar.setText(resourceMap.getString("BTN_Buscar.text")); // NOI18N
        BTN_Buscar.setName("BTN_Buscar"); // NOI18N
        BTN_Buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BTN_BuscarActionPerformed(evt);
            }
        });
        jPanel1.add(BTN_Buscar);
        BTN_Buscar.setBounds(210, 40, 73, 23);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BTN_BuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_BuscarActionPerformed
        JInternalFrame jif_Profesiones_Buscar;
        JP_Profesiones_Buscar panel = new JP_Profesiones_Buscar();
        jif_Profesiones_Buscar = Utiles_Panel.creaVentanaInternaModal((UDPRRHHView) this.getRef(), panel, "Busca de Especialidades");
        panel.setPadre(Utiles_Panel.TIPO_JINTERNALFRAME, jif_Profesiones_Buscar);
        panel.setRefDep(this);
    }//GEN-LAST:event_BTN_BuscarActionPerformed

    private void BTN_NuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_NuevoActionPerformed
        this.opcion = "nuevo";
        BTN_Modificar.setEnabled(false);
        BTN_Buscar.setEnabled(false);
        BTN_Nuevo.setEnabled(false);
        TXT_Profesion.setEditable(true);
        TXT_Abreviatura.setEditable(true);
        BTN_Guardar.setEnabled(true);
        BTN_Cancelar.setEnabled(true);
        limpiarCampos();
    }//GEN-LAST:event_BTN_NuevoActionPerformed

    private void BTN_GuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_GuardarActionPerformed
//        String cadena = "",codigo="";
//        Connection cn = null;
//        Statement st;
//        ResultSet rs;
        try {

            if (this.opcion.compareTo("nuevo") == 0) {

                //obtener el siguiente codigo
//                cadena = "select count(*)+1 as codigo from profesion ";
//                cn = Cls_Conexion.obtenerConexion();
//                st = cn.createStatement();
//                rs = st.executeQuery(cadena);
//                while(rs.next()){
//                    codigo = rs.getString("codigo").trim();
//                    for(int i=codigo.length();i<3;i++){
//                        codigo = "0"+codigo;
//                    }
//                }
//                cadena = "insert into profesion(cod_profesion,descripcion,abreviatura) ";
//                cadena = cadena + "values('"+codigo+"','"+TXT_Profesion.getText()+"','"+TXT_Abreviatura.getText()+"')";
                if (esFormularioValido()) {
                    String parametrosParaService_01[] = {Cls_Parametros.Map_PROFESION, TXT_Profesion.getText().toUpperCase()};
                    String parametrosParaService_02[] = {Cls_Parametros.Map_ABREVIATURA, TXT_Abreviatura.getText().toUpperCase()};

                    JOptionPane.showMessageDialog(this, Cls_InvocaWebService.nuevaProfesion(Cls_Funciones.
                            generarHashMapParaSolicitud(parametrosParaService_01, parametrosParaService_02)));

                    this.opcion = "";
                    resetearFormulario();
                }

            } else {
                if (this.opcion.compareTo("modificar") == 0) {
//                    cadena = "update profesion set descripcion='"+TXT_Profesion.getText()+"',";
//                    cadena = cadena + "abreviatura='"+TXT_Abreviatura.getText()+"' where cod_profesion='"+TXT_Codigo.getText()+"'";

                    if (esFormularioValido()) {
                        String parametrosParaService_01[] = {Cls_Parametros.Map_CODIGO_PROFESION, TXT_Codigo.getText()};
                        String parametrosParaService_02[] = {Cls_Parametros.Map_PROFESION, TXT_Profesion.getText().toUpperCase()};
                        String parametrosParaService_03[] = {Cls_Parametros.Map_ABREVIATURA, TXT_Abreviatura.getText().toUpperCase()};

                        JOptionPane.showMessageDialog(this, Cls_InvocaWebService.modificarProfesion(Cls_Funciones.
                                generarHashMapParaSolicitud(parametrosParaService_01, parametrosParaService_02, parametrosParaService_03)));

                        this.opcion = "";
                        resetearFormulario();
                    }
                }
            }
//            if ( this.opcion.compareTo("modificar") == 0 ){
//                cn = Cls_Conexion.obtenerConexion();
//            }
//            st = cn.createStatement();
//            if(st.executeUpdate(cadena) == 1){
//                JOptionPane.showMessageDialog(this, "Operacion Satisfatoria");
//            }else{
//                JOptionPane.showMessageDialog(this, "Errro en la Operacion");
//            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_BTN_GuardarActionPerformed

    public boolean esFormularioValido() {
        TXT_Abreviatura.setText(TXT_Abreviatura.getText().trim());
        TXT_Profesion.setText(TXT_Profesion.getText().trim());
        if (TXT_Profesion.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar una profesión válida");
            return false;
        }

        if (TXT_Abreviatura.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Debe ingresar una abreviatura válida");
            return false;
        }

        return true;
    }
    
    public void limpiarCampos(){
        TXT_Codigo.setText("");
        TXT_Profesion.setText("");
        TXT_Abreviatura.setText("");
    }

    private void BTN_ModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_ModificarActionPerformed
        this.opcion = "modificar";
        BTN_Nuevo.setEnabled(false);
        BTN_Modificar.setEnabled(false);
        BTN_Buscar.setEnabled(false);
        TXT_Profesion.setEditable(true);
        TXT_Abreviatura.setEditable(true);
        BTN_Guardar.setEnabled(true);
        BTN_Cancelar.setEnabled(true);
    }//GEN-LAST:event_BTN_ModificarActionPerformed

    private void BTN_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BTN_CancelarActionPerformed
        resetearFormulario();
    }//GEN-LAST:event_BTN_CancelarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BTN_Buscar;
    private javax.swing.JButton BTN_Cancelar;
    private javax.swing.JButton BTN_Guardar;
    private javax.swing.JButton BTN_Modificar;
    private javax.swing.JButton BTN_Nuevo;
    private javax.swing.JTextField TXT_Abreviatura;
    private javax.swing.JTextField TXT_Codigo;
    private javax.swing.JTextField TXT_Profesion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

}
